﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Administrators : AdminPage, IAdministratorsView
    {
        private AdministratorsPresenter _presenter;

        #region IAdministratorsView Members

        public void SetAdministrators(IList<Administrator> administrators)
        {
            rpAdministrators.DataSource = administrators;
            rpAdministrators.DataBind();
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Administrators.xml"; }
        }

        public void DeleteLastAdministratorAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }

        public void DeleteLastAdministratorInTheAdminGroupAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteLastAdministratorAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new AdministratorsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('adminPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }
        
        protected void rpAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var admin = (Administrator) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");
            var litGroup = (Literal) e.Item.FindControl("litGroup");
            var litAvailableSites = (Literal) e.Item.FindControl("litAvailableSites");
            var litDefaultSite = (Literal) e.Item.FindControl("litDefaultSite");
            litID.Text = "<span id=\"A" + admin.ID + "\">";
            litName.Text = admin.Fullname;
            litGroup.Text = admin.Group.Name;
            litDefaultSite.Text = admin.DefaultSite != null ? admin.DefaultSite.Domain : "&nbsp;";
            foreach (Site s in admin.AvailableSites)
            {
                if (!string.IsNullOrEmpty(litAvailableSites.Text))
                    litAvailableSites.Text += ", ";
                litAvailableSites.Text += s.Domain;
            }
        }
        
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.Delete(int.Parse(hidDeleteID.Value.Substring(1)));
            upnlAdministrators.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList", "initList();", true);
        }
    }
}